package com.hrvsr.session.proxy;

import com.hrvsr.common.redis.AbstractRedisQueue;
import com.hrvsr.common.util.SerializationUtil;

import redis.clients.jedis.Jedis;

public class RedisProxyPool extends AbstractRedisQueue implements ProxyPool {
	@Override
	public Proxy poll() {
		Jedis jedis = genJedis();
		try {
			byte[] bytes = jedis.rpop(getQueue().getBytes());
			Proxy proxy = SerializationUtil.unSerialize(bytes, Proxy.class);
			return proxy;
		} finally {
			jedis.close();
		}
	}

	@Override
	public void offer(Proxy proxy) {
		Jedis jedis = genJedis();
		try {
			byte[] bytes = SerializationUtil.serialize(proxy);
			jedis.lpush(getQueue().getBytes(), bytes);
		} finally {
			jedis.close();
		}
	}
}
